package fr.pioug.gdocs.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.crypto.Cipher;


/**
 * @author a011228
 * @version
 */
public class ConstantesService {

	private static ConstantesService mConstanteService = null;

	private Properties mProperties;

	/**
	 */
	public static final String FILE_CONSTANTE = "constantes.properties";

	private ConstantesService() {
		mProperties = new Properties();
		load();
	}

	private void load() {
		try {
			File lFile = new File(System.getProperty("user.home")
					+ File.separator + FILE_CONSTANTE);
			if (lFile.exists()) {
				mProperties.load(new FileInputStream(lFile));
			} else {
				mProperties.load(this.getClass().getClassLoader()
						.getResourceAsStream(FILE_CONSTANTE));
				initDefault();
				save();
			}

		} catch (FileNotFoundException lException) {
			LogService.error(lException.getMessage(), ConstantesService.class,
					lException);
		} catch (IOException lException) {
			LogService.error(lException.getMessage(), ConstantesService.class,
					lException);
		}
	}

	private static void initDefault() {

	}

	/**
	 * 
	 */
	public void save() {
		FileOutputStream lFileOutputStream = null;
		try {
			File lFile = new File(System.getProperty("user.home")
					+ File.separator + FILE_CONSTANTE);
			lFileOutputStream = new FileOutputStream(lFile);
			mProperties.store(lFileOutputStream, null);
			lFileOutputStream.close();
			LogService.debug("Config file save " + lFile.getAbsolutePath(),
					ConstantesService.class);
		} catch (FileNotFoundException lException) {
			LogService.error(lException.getMessage(), ConstantesService.class,
					lException);
		} catch (IOException lException) {
			LogService.error(lException.getMessage(), ConstantesService.class,
					lException);
		} finally {
			if (lFileOutputStream != null) {
				try {
					lFileOutputStream.close();
				} catch (IOException lException) {
					LogService.error(lException.getMessage(),
							ConstantesService.class, lException);
				}
			}
		}
	}

	/**
	 * @return
	 */
	public static ConstantesService getInstance() {
		if (mConstanteService == null) {
			mConstanteService = new ConstantesService();
		}
		return mConstanteService;
	}

	/**
	 * @param pKey
	 * @return
	 */
	public String getValue(String pKey) {
		return mProperties.getProperty(pKey);
	}

	// /**
	// * @param pKey
	// * @return
	// */
	// public String[] getArrayValues(String pKey) {
	// return StringUtil.split(mProperties.getProperty(pKey), ",");
	// }

	/**
	 * @param pKey
	 * @param pValue
	 */
	public void set(String pKey, String pValue) {
		mProperties.setProperty(pKey, pValue);

	}

	/**
	 * @param pValue
	 * @return
	 */
	public static String encrypt(String pValue) {
		return crypt(pValue, Cipher.ENCRYPT_MODE);
	}

	private static String crypt(String pValue, int pType) {
		// String lValue = null;
		// try {
		// Cipher lCipher;
		// lCipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
		//
		// lCipher.init(pType, new SecretKeySpec("GDocsSync".getBytes(),
		// "Blowfish"));
		// lValue = lCipher.doFinal(pValue.getBytes()).toString();
		// } catch (Exception lException) {
		// // TODO Auto-generated catch block
		// lException.printStackTrace();
		// }
		// return lValue;
		return pValue;
	}

	/**
	 * @param pValue
	 * @return
	 */
	public static String decrypt(String pValue) {
		return crypt(pValue, Cipher.DECRYPT_MODE);
	}
}
